<?php 
session_start(); 
if(!isset($_SESSION['IsLogin']))
{
	die("Thank you for your using this application! </br> To reaccess this page, you need to <a href='index.php'>LOGIN</a>"); // Make sure they are logged in!
}
else
{
	echo "<h3>Hello ".$_SESSION['IsLogin']."!</h3>";
	print "<form action ='buy.php' method='post'>";
	print '<div class="cart_title"></div>';
	print  "<input type= 'submit' name= 'logout' value= 'Log out'>";
	if(isset($_POST['logout']))
	{
		session_destroy();
		Header("Location: buy.php");
		
	}
	
	print "</form>";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Shop</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="css/iecss.css" />
<![endif]-->
<script type="text/javascript" src="js/boxOver.js"></script>
<script type="text/javascript">  

</script>
</head>
<body>
<div id="main_content"> 
   
            <div id="menu_tab">
            <div class="left_menu_corner"></div>
                    <ul class="menu">
                         <li><a href="index.php" class="nav1">  Home </a></li>
						 <li><a href="invoice.php" class="nav1">  Invoice </a></li>
                         <li class="divider"></li>
                    </ul>

             <div class="right_menu_corner"></div>
            </div><!-- end of menu tab -->

<?php
//Bring product arrays
include 'product_data.inc';

//define functions
require_once 'functions.inc';
$all_users=  arrayfile_to_array('./user_data.dat');


?>
<?php
$email='';
if ($actual_user_key=array_key_exists_nc($_SESSION['IsLogin'], $all_users)) {
	//found the user, now go get user information.
    $user_info = $all_users[$actual_user_key];
	print '<h3>This is your information: </h3></br>';
	print 'First name: '.$user_info['firstName'].'</br>';
	print 'Last name: '.$user_info['lastName'].'</br>';
	print 'Email: '.$user_info['email'].'</br>';
	$email = $user_info['email'];
	print 'Grand Total: '.$_SESSION["total"].'</br>';
	print 'Credit card number: <input type="text" name="cardnumber"></br>';
	print 'Expiration date: <input type="text" name="date"></br>';
	print 'Address: <input type="text" name="address"></br>';
		
	print "<h3>This is your shopping cart, ".$_SESSION['IsLogin'] . ".</h3>";
	print "<br>";
	print "<font size= 5><b>INVOICE</b>";
	print "<br>";
	print "<table border= '1' cellpadding= '5' cellspacing= '0'>";
	print '<th>Name</th> <th>Quantity Ordered</th> <th>Price Per Item</th> <th>Extended Price</th>';
        
	//This starts the invoice total as $0 and then adds to the total.
	
	$total = 0;
	$qty_ordered= 0;
	$subtotal=0;
	//print "<form action ='buy.php' method='post'>"; 
			
		
	for ($i= 0; $i < count($products); $i++) {
		$qty= @$_SESSION["quantity$i"];
	
		// This checks to see if the user inputted an integer.
		if ($qty> 0 && $qty- (int) $qty== 0) {
			//if(array_key_exists('delete$i',$_POST)){
			//$qty = $qty-1;
			//}
	        
			// This calculates the price if the user orders more than 1 item.
			$extended_price = $qty*$products[$i]['Price'];
			
			// This calculates the total price before shipping and tax.
			$total = $total+$extended_price;
				 
			// This displays the data I want in a table.
			print "<tr><td colspan= '1'>{$products[$i]['Name']}</td><td colspan= '1'>$qty</td>";
			print "<td colspan= '1'>";
			printf('$%.2f',"{$products[$i]['Price']}");
			print "</td>";
			print "<td colspan= '1'>";
			printf('$%.2f',($extended_price));
			print "</td></tr>";
			print "</table";       
        	
		}
        
	}

	// Here, I want to calculate extra costs such as taxes.
	// The tax in Hawaii is 4.72%.
	$tax= .0472;
	$tax_amount= $tax*$total;

	print "<tr><td colspan= '3'><b>Subtotal</b></td>";
	print "<td colspan= '1'>";
    //This is the correct format of prices.
    printf('$%.2f', $total);
    print "</td>";
	print "<tr><td colspan= '3'><b>Tax</b></td>";
	print "<td colspan= '1'>";
    printf('$%.2f',($tax_amount));
	print "</td>";
	print "<tr><td colspan='3'><b>Shipping</b></td>";


	// I included different shipping rates.
	// I believe that shipping costs influence buyers.
	$shipping = 0;
	if($total > 0) $shipping = 10;
	if($total > 100) $shipping = 25;
	if($total > 200) $shipping = 0;
	print "<td colspan= '1'>";
    printf('$%.2f',$shipping);
    print "</td></tr>";


	$subtotal= $total+$tax_amount+$shipping;

	print "<tr><td colspan='3'><b>Grand Total</b></td>";
	print "<td colspan= '1'>";
            printf('$%.2f',$subtotal);
        print "</td></tr>";
		
	
	print "<input type=hidden name=total value=$subtotal>";	
	
	print "<form action ='email.php' method='post'>";
	print "<input type=hidden name=email value=$email>";            
	print '<input type="submit" name="submit" value="Done"></br>';

}


?>
